package com.ljk.study13ormdynamicdatasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ljk.study13ormdynamicdatasource.entity.User;
import com.ljk.study13ormdynamicdatasource.mapper.UserMapper;
import com.ljk.study13ormdynamicdatasource.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author ljk
 * @date 2021/4/15 22:51
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void insert4DB1() {
        for (int i=0;i<3;i++) {
            User user = User.builder().name("数据库1 name"+i).password("password").salt("salt").email("email"+i).phoneNumber("phoneNum"+i).status(1).createTime(new Date()).lastUpdateTime(new Date()).build();
            userMapper.saveUser(user);
        }
    }

    @Override
    public List<User> findAllByDB1() {
        return userMapper.selectAllUser();
    }

    @Override
    @DS("slave_1")
    public void insert4DB2() {
        for (int i=0;i<3;i++) {
            User user = User.builder().name("数据库2 name"+i).password("password").salt("salt").email("email"+i).phoneNumber("phoneNum"+i).status(1).createTime(new Date()).lastUpdateTime(new Date()).build();
            userMapper.saveUser(user);
        }
    }

    @Override
    @DS("slave_1")
    public List<User> findAllByDB2() {
        return userMapper.selectAllUser();
    }
}
